﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LightManager.Effects;
using LightManager.UserControls;

namespace LightManager
{
    public partial class Form1 : Form
    {
        GameManager manager = new GameManager();

        public Form1()
        {
            InitializeComponent();

            Unit ste = new Unit() { Name = "Nala", PlayerName = "Stefania" };
            ste.Initialize(20);
            ste.Armors[DamageSources.Phisical] = 2;

            Unit aldo = new Unit() { Name = "Alariel", PlayerName = "Aldo" };
            aldo.Initialize(20);
            aldo.Armors[DamageSources.Phisical] = 3;
            aldo.Shields[DamageSources.Phisical] = 15;
            aldo.ImageFile = "graphic\\aldo.png";

            Unit leti = new Unit() { Name = "Erinne", PlayerName = "Letizia" };
            leti.Initialize(20);
            leti.ImageFile = "graphic\\leti.png";

            Unit nanny = new Unit() { Name = "Findo", PlayerName = "Nanny" };
            nanny.Initialize(20);
            nanny.Armors[DamageSources.Phisical] = 1;

            manager.Units.Add(ste);
            manager.Units.Add(aldo);
            manager.Units.Add(leti);
            manager.Units.Add(nanny);

            this.commandLineControl.Initialize(this.manager);
            this.effectListControl1.Manager = this.manager;

            foreach (Unit u in this.manager.Units)
                this.AddUnitControl(u);

            this.manager.UnitChanged += this.manager_UnitChanged;
            this.manager.Units.ListChanged += this.Units_ListChanged;

        }

        void Units_ListChanged(object sender, ListChangedEventArgs e)
        {
            if (e.ListChangedType == ListChangedType.ItemAdded)
                this.AddUnitControl(this.manager.Units[e.NewIndex]);
            if (e.ListChangedType == ListChangedType.ItemDeleted)
                this.UnitCheck();
        }

        private void UnitCheck()
        {
            List<IUnitRenderer> controls = new List<IUnitRenderer>();
            controls.AddRange(this.flpPlayers.Controls.OfType<IUnitRenderer>());
            controls.AddRange(this.flpUnits.Controls.OfType<IUnitRenderer>());
            foreach (IUnitRenderer c in controls)
            {
                if (this.manager.Units.Contains(c.DataSource)) continue;
                this.flpPlayers.Controls.Remove((Control)c);
                this.flpUnits.Controls.Remove((Control)c);
            }
        }

        private void AddUnitControl(Unit unit)
        {
            if (unit.IsEnemy)
            {
                UnitControl m = new UnitControl();
                m.DataSource = unit;
                this.flpUnits.Controls.Add(m);
                return;
            }
            PlayerControl p = new PlayerControl();
            p.DataSource = unit;
            this.flpPlayers.Controls.Add(p);
        }

        void manager_UnitChanged(Unit data)
        {
            foreach (Control c in this.flpUnits.Controls)
                if (c is IUnitRenderer)
                    if (((IUnitRenderer)c).DataSource.Id == data.Id)
                        ((IUnitRenderer)c).DataSource = data;
            foreach (Control c in this.flpPlayers.Controls)
                if (c is IUnitRenderer)
                    if (((IUnitRenderer)c).DataSource.Id == data.Id)
                        ((IUnitRenderer)c).DataSource = data;
        }
    }
}
